Shader "Kun URP/TestToonLit"
{
    Properties
    {
        _MainTex ("MainTex", 2D) = "white" {}
        _RampTex ("RampTex", 2D) = "white" {}
        _IlmTex ("IlmTex", 2D) = "white" {}

        [Space(20)]
		_MainColor("Main Color", Color) = (1,1,1)
		_ShadowColor ("Shadow Color", Color) = (0.7, 0.7, 0.7)
		_ShadowSmooth("Shadow Smooth", Range(0, 0.03)) = 0.002
		_ShadowRange ("Shadow Range", Range(0, 1)) = 0.6
        _ShadowEdge0 ("Shadow Edge 0", Range(0, 1)) = 0
        _ShadowEdge1 ("Shadow Edge 1", Range(0, 1)) = 1
        _WrapLambert ("Wrap Lambert", Range(0, 1)) = 0.5

        [Space(20)]
		_SpecularColor("Specular Color", Color) = (1,1,1)
		_SpecularRange ("Specular Range",  Range(0, 1)) = 0.9
        _SpecularMulti ("Specular Multi", Range(0, 1)) = 0.4
		_SpecularGloss("Sprecular Gloss", Range(0.001, 8)) = 4

        [Space(10)]
	    _OutlineWidth ("Outline Width", Range(0.00, 1)) = 0.24
        _OutlineColor ("OutLine Color", Color) = (0.5,0.5,0.5,1)
    }
    SubShader
    {
        Pass
        {
            Name "Outline"
            Tags {

            }
            Cull Front

            HLSLPROGRAM

            // #pragma shader_feature_local _ENABLE_OUTLINE_ON

            #include "../../ShaderLibrary/ToonLit/ToonLitInput.hlsl"
            #include "../../ShaderLibrary/OutlinePass.hlsl"

            #pragma vertex OutlinePassVertex
            #pragma fragment OutlinePassFragment
            
            ENDHLSL
        }


        Pass
        {
            Name "LitForward"
            Tags {
                 "LightMode" = "UniversalForward"
            }
            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            // #pragma prefer_hlslcc gles
            // #pragma exclude_renderers d3d11_9x

            #pragma vertex vert
            #pragma fragment frag
            // #pragma shader_feature _ALPHATEST_ON
            // #pragma shader_feature _ALPHAPREMULTIPLY_ON

            // // -------------------------------------
            // // Unity defined keywords
            // #pragma multi_compile_fog
            // #pragma multi_compile_instancing

            // #include "UnlitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            struct Attributes
            {
                float4 positionOS       : POSITION;
                float3 normalOS         : NORMAL;
                float2 uv               : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct Varyings
            {
                float4 positionCS       : SV_POSITION;
                float2 uv               : TEXCOORD0;
                float3 normalWS         : TEXCOORD1;
		        float3 positionWS       : TEXCOORD2; 
                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _MainTex; 
	        float4 _MainTex_ST;
            sampler2D _RampTex; 
	        float4 _RampTex_ST;
            sampler2D _IlmTex; 
			float4 _IlmTex_ST;

            half3 _MainColor;
			half3 _ShadowColor;
			half _ShadowSmooth;
			half _ShadowRange;
            half _ShadowEdge0;
            half _ShadowEdge1;
            half _WrapLambert;
			
			half3 _SpecularColor;
			half _SpecularRange;
        	half _SpecularMulti;
			half _SpecularGloss;

            Varyings vert(Attributes input)
            {
                Varyings output = (Varyings)0;

                UNITY_SETUP_INSTANCE_ID(input);
                UNITY_TRANSFER_INSTANCE_ID(input, output);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

                VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
                output.positionCS = vertexInput.positionCS;
                output.positionWS = vertexInput.positionWS;
                output.normalWS = TransformObjectToWorldNormal(input.normalOS);
                output.uv = input.uv;

                return output;
            }

            half4 frag(Varyings input) : SV_Target
            {
                half4 col = 1;
                half4 mainTex = tex2D(_MainTex, input.uv);
                half4 ilmTex = tex2D (_IlmTex, input.uv);

                float3 positionWS = input.positionWS;
                float3 normalWS = input.normalWS;
                float3 viewDirWS = normalize(_WorldSpaceCameraPos.xyz - positionWS);
                Light mainLight = GetMainLight();
                float3 lightDirWS = mainLight.direction;

                // float halfLambert = (dot(normalWS, lightDirWS) * _WrapLambert + _WrapLambert);
                // half threshold = (halfLambert + ilmTex.g) * mainLight.shadowAttenuation;
                // // half ramp = saturate(_ShadowRange - threshold);
                // // ramp =  smoothstep(0, _ShadowSmooth, ramp);
                // half ramp = saturate(smoothstep(_ShadowEdge0, _ShadowEdge1, threshold));
                // half3 diffuse = lerp(_MainColor, _ShadowColor, ramp);
				// diffuse = float3(ilmTex.r, ilmTex.r, ilmTex.r);

                half3 diffuse = 0;
				half halfLambert = dot(normalWS, lightDirWS) * 0.5 + 0.5;
				half threshold = (halfLambert + ilmTex.g) * 0.5;
				half ramp = saturate(_ShadowRange  - threshold); 
				ramp =  smoothstep(0, _ShadowSmooth, ramp);
				diffuse = lerp(_MainColor, _ShadowColor, ramp);
				diffuse *= mainTex.rgb;

                // half f =  1.0 - saturate(dot(viewDirWS, normalWS));
                // half rim = smoothstep(_RimMin, _RimMax, f);
                // rim = smoothstep(0, _RimSmooth, rim);
                // half3 rimColor = rim * _RimColor.rgb *  _RimColor.a;

                half3 specular = 0;
				half3 halfDir = normalize(lightDirWS + viewDirWS);
				half NdotH = max(0, dot(normalWS, halfDir));
				half SpecularSize = pow(NdotH, _SpecularGloss);
				half specularMask = ilmTex.b;
				if (SpecularSize >= 1 - specularMask * _SpecularRange)
				{
					specular = _SpecularMulti * (ilmTex.r) * _SpecularColor;
				}

                col.rgb = (diffuse + specular);
                // col.rgb = float3(ramp, ramp, ramp);
                return col;
            }
            ENDHLSL
        }

        Pass
        {
            Tags{"LightMode" = "DepthOnly"}

            ZWrite On
            ColorMask 0

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma target 2.0

            #pragma vertex DepthOnlyVertex
            #pragma fragment DepthOnlyFragment

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature _ALPHATEST_ON

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing

            #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
            ENDHLSL
        }
    }
}
